﻿@using Ext.Net
@using Ext.Net.MVC
@{
    ViewBag.Title = "Lookup";
    Layout = "~/Views/Shared/_Layout.cshtml";
}
@section Header
{
    <script type="text/javascript">
        function lookup() {
            if (App.DataGridPanel.getSelectionModel().getSelection()[0] == null) {
                alert('No address selected');
                return;
            }
            var value = App.DataGridPanel.getSelectionModel().getSelection()[0].get('Street');
            var id = App.DataGridPanel.getSelectionModel().getSelection()[0].get('AddressId');
            alert(value + '-' + id);
            parent.Ext.WindowMgr.getActive().hide();
            parent.Ext.getCmp('@Request.QueryString["text"]').setValue(value);
            parent.Ext.getCmp('@Request.QueryString["id"]').setValue(id);
        }
    </script>
}
@Html.X().FieldSet().ID("SearchForm").Title("Search address").Collapsible(true).Layout(LayoutType.Form).Items(
    Html.X().FieldContainer().Layout(LayoutType.HBox).Items(
                Html.X().TextField().ID("txtPostCode").FieldLabel("PostCode").Flex(1),
                Html.X().TextField().ID("txtStreet").FieldLabel("Street").Flex(1).Margins("0 0 0 10"),
                Html.X().TextField().ID("txtTown").FieldLabel("Town").Flex(1).Margins("0 0 0 10")
        ),

    Html.X().FieldContainer().Layout(LayoutType.HBox).Items(
        Html.X().Button().Text("Search").Icon(Icon.Find).DirectEvents(
            de =>
            {
                de.Click.Url = Url.Action("Search");
                de.Click.FormID = "SearchForm";
            }),
            Html.X().Button().Text("None").Margins("0 0 0 10").DirectEvents(
                de =>
                {
                    de.Click.Url = Url.Action("ClearSearch");
                    de.Click.FormID = "SearchForm";
                }),
        Html.X().Button().Text("Clear").Margins("0 0 0 10").Listeners(ls =>
                ls.Click.Handler = "App.txtPostCode.setValue('');App.txtStreet.setValue('');App.txtTown.setValue('');"
        )
        )

     )
@* GridPanel *@
@(Html.X().GridPanel().ID("DataGridPanel")
        .Title("Select address")
        .Icon(Icon.Table)
        .Frame(true)
        .Store(Html.X().Store().ID("AddressStore")
                .Model(Html.X().Model().IDProperty("AddressId")
                .Fields(
                    new ModelField("AddressId", ModelFieldType.Int),
                    new ModelField("Street"),
                    new ModelField("PostCode"),
                    new ModelField("Town"),
                    new ModelField("County"),
                    new ModelField("Country")
                )
            )
            .RemoteSort(true)
            .RemotePaging(true)
            .PageSize(2)
            .Proxy(
                        Html.X().AjaxProxy()
                            .Url(Url.Action("BindingStore"))
                            .Reader(Html.X().JsonReader().Root("data"))
                    )


        )
        .ColumnModel(
                    Html.X().Column().Text("ID").DataIndex("AddressId").Hidden(true),
                        Html.X().Column().Text("Address").DataIndex("Street").Width(400),
                    Html.X().Column().Text("PostCode").DataIndex("PostCode"),
                    Html.X().Column().Text("Town").DataIndex("Town"),
                    Html.X().Column().Text("County").DataIndex("County"),
                    Html.X().Column().Text("Country").DataIndex("Country")
        )
        .BottomBar(Html.X().PagingToolbar())
)
<div style="margin-top: 10px">
    @Html.X().ButtonGroup().Items(
        Html.X().Button().Text("Select").Icon(Icon.Accept).Listeners(
                                                li => li.Click.Handler = "lookup();"
        ),
                Html.X().Button().Text("Close").Icon(Icon.Decline).Listeners(ls => ls.Click.Handler = "parent.Ext.WindowMgr.getActive().hide();")
         )
</div>
